import { createRouter, createWebHistory } from 'vue-router'
import PkIndexView from '@/views/pk/PkIndexView'
import rankListIndexView from '@/views/rankList/rankListIndexView'
import recordContentView from '@/views/record/RecordContentView'
import recordIndexView from '@/views/record/recordIndexView'
import userSnakeIndexView from '@/views/user/bot/userSnakeIndexView'
import NotFound from '@/views/error/NotFound'
import UserAccountLoginView from '@/views/user/account/UserAccountLoginView'
import UserAccountRegisterView from '@/views/user/account/UserAccountRegisterView'
import store from '@/store'

const routes = [
  {
    path: "/",
    name: "home",
    redirect: "/pk/",
    meta: {
      requestAuth: true, // 表示这个页面是否需要授权登录
    }
  },
  {
    path: "/pk/",
    name: "pk_index",
    component: PkIndexView,
    meta: {
      requestAuth: true, // 表示这个页面是否需要授权登录
    }
  },
  {
    path: "/record/",
    name: "record_index",
    component: recordIndexView,
    meta: {
      requestAuth: true, // 表示这个页面是否需要授权登录
    }
  },
  {
    path: "/record/:recordId/",
    name: "record_content",
    component: recordContentView,
    meta: {
      requestAuth: true, // 表示这个页面是否需要授权登录
    }
  },
  {
    path: "/rankList/",
    name: "rankList_index",
    component: rankListIndexView,
    meta: {
      requestAuth: true, // 表示这个页面是否需要授权登录
    }
  },
  {
    path: "/user/snake/",
    name: "userSnake_index",
    component: userSnakeIndexView,
    meta: {
      requestAuth: true, // 表示这个页面是否需要授权登录
    }
  },
  {
    path: "/NotFound/",
    name: "NotFound_index",
    component: NotFound,
  },
  {
    path: "/user/account/login/",
    name: "user_account_login",
    component: UserAccountLoginView,
    meta: {
      requestAuth: false, // 表示这个页面是否需要授权登录
    }
  },
  {
    path: "/user/account/register/",
    name: "user_account_register",
    component: UserAccountRegisterView,
    meta: {
      requestAuth: false, // 表示这个页面是否需要授权登录
    }
  },
  {
    path: "/:catchAll(.*)",
    redirect: "/NotFound/",
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => { // 在路由跳转之前执行这个函数, next是需要执行的下一步擦欧总
  if (to.meta.requestAuth && !store.state.user.is_login) {
    next({name: "user_account_login"});
  } else {
    next();
  }
})

export default router
